/**
 * services/uploader
 */

// import lodash from 'lodash';
// import Promise from 'bluebird';

define([
    'lodash',
    'bluebird',
    'common/dcf',
    './qiniu',
    '../utils/file',
    '../filters/qn-url'
], function(
    lodash, Promise, DCF,
    qiniu, fileTools,
    qnUrl
) {

    function Uploader() {
        // @TODO: 多实例
    }

    // 静态方法
    lodash.assign(Uploader, {
        uid: 0,
        typeExtMap: {
            jpeg: 'jpg'
        },
        getExtByType: function(type) {
            var subType = lodash.trim(type.split('/')[1]);

            return this.typeExtMap[subType] || subType;
        },
        processes: {
            blob: lodash.identity,
            base64: function(data) {
                return fileTools.dataURLToBlob(data);
            },
            text: function(text, options) {
                var mimeTypesMap = {
                    svg: 'text/svg+xml'
                };

                var mimeType = mimeTypesMap[options.type];
                if(!mimeType) {
                    mimeType = 'text/plain';
                }

                return fileTools.textToBlob(text, {
                    type: mimeType
                });
            }
        },
        upload: Promise.method(function(data, options) {
            // options
            if(typeof options === 'string') {
                options = {
                    key: options
                };
            }
            options = lodash.assign({
                dataType: 'blob'
            }, options);

            // data process
            var self = this;
            var dataType = options.dataType;
            var dataProcess = this.processes[dataType];

            return Promise.try(function() {
                if(!dataProcess) {
                    throw new Error('No upload process defined: ' + dataType);
                }

                return dataProcess(data, options);
            })
            // key fix
            .then(function(file) {
                // auto prefix
                var key = lodash.trim(options.key);
                if(!key) {
                    var uriArr = location.pathname.split('/');
                    var prefix = 'www/' + DCF.userId + '/';
                    prefix += uriArr.slice(1, 3).join('/');

                    key = prefix + '/';
                }

                // auto filename
                // 如果以 / 结尾，则自动补全文件名
                if(key.slice(-1) === '/') {
                    var now = new Date();

                    var filename = now.getFullYear();
                    filename += ('00' + now.getMonth()).slice(-2);
                    filename += ('00' + now.getDate()).slice(-2);
                    filename += '-';
                    filename += ('00' + now.getHours()).slice(-2);
                    filename += ('00' + now.getMinutes()).slice(-2);
                    filename += ('00' + now.getSeconds()).slice(-2);

                    filename += '-';
                    filename += (++self.uid);
                    filename += '.' + self.getExtByType(file.type);

                    key += filename;
                }

                // upload
                // return {key: 'x/y/z'};
                return qiniu.upload(file, key);
            })
            .then(function(res) {
                // add url
                res.url = qnUrl(res.key);

                return res;
            });
        })
    });

    return Uploader;
});